from datetime import datetime
import requests
MONITOR_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e991632d-138f-446f-b68f-9cdb86bb5f70"


def monitor_sender(name, msg):
    """
    企微消息发送模块
    """
    headers = {
        'Content-Type': 'application/json',
    }
    content = f"👉任务: {name}\n👉报告时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n👉事件描述:{msg}"
    json_data = {
        'msgtype': 'text',
        'text': {
            'content': content,
            "mentioned_list": ["18826237037"],
        },
    }

    response = requests.post(
        MONITOR_URL,
        headers=headers,
        json=json_data
    )
    return response.status_code


def monitor_main():
    url = "http://110.41.12.238/api/get_all_endpoints_state"
    response = requests.get(url)
    data = response.json()
    msg = ""
    for endpoint, value in data.items():
        msg += f"""
        🔥接口：/api/{endpoint},
        ✊被调用次数：{value['total_calls']},
        👌调用成功次数：{value['successful_calls']},
        ❗️调用失败次数：{value['failed_calls']}
        """
    monitor_sender("数据服务接口监控", msg)


if __name__ == '__main__':
    monitor_main()
